(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.buyBack.createContinuePreviewController', createContinuePreviewController);
	createContinuePreviewController.$inject = [ '$q', '$filter', '$scope', 'Constants', 'formatService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'buyBackService', 'dateService', "arithService", 'commonService'];
	function createContinuePreviewController($q, $filter, $scope, Constants, formatService, modalService, gridService, layoutConfig, validatorConfig, authService, buyBackService, dateService, arithService, commonService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.invPayLoad = {};
		ctrl.init = function() {
			buyBackService.getCurrencyOptions().then(function(response) {
				ctrl.currencyOptions = response.data;
			});
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.pisCfg.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.invPayLoad.piList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			ctrl.invPayLoad= params.invPayLoad;
		};

		ctrl.createContinueSubmit = function() {
			if (!ctrl.invPayLoad.piList || ctrl.invPayLoad.piList.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.pi.norecsel' ]
				});
				return false;
			}
			return authService.createContinueSubmit(ctrl.invPayLoad).then(submitSuccessful, submitFailed);
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.buyback.create',
					'state' : 'apf.buyBack.create',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);
